/**
 * Copyright (c) 2015-2015 All Rights Reserved.
 */
package com.zbest.zpay.exchange.helper;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.zbest.common.lang.enums.SystemCode;
import com.zbest.common.lang.util.CoreDateUtils;
import com.zbest.component.dal.sequence.DefaultSequenceFactory;

/**
 * 序列号生成器
 * @author hangdahong
 * @version $Id: SeqHelper.java, v 0.1 2015年1月22日 上午9:50:30 hangdahong Exp $
 */
@Service("seqHelper")
public class SeqHelper {

    /**支付流水号*/
    private static final String    PAYCORE_PAY_INFO_ID     = "PAY_INFO_ID";

    /**支付子流水号*/
    private static final String    PAYCORE_PAY_SUB_INFO_ID = "PAY_SUB_INFO_ID";

    /**交换流水号*/
    private static final String    FLUX_INFO_ID            = "FLUX_ID";

    private static final String    BATCH_NO                = "BATCH_NO";

    @Resource
    private DefaultSequenceFactory paycoreDefaultSequenceFactory;

    private SeqHelper() {

    }

    /**
     * 支付流水号,规则：日期14位+3位系统编码+8位流水号
     * 
     * @return
     */
    public String genPayInfoId() {
        Assert.notNull(paycoreDefaultSequenceFactory);

        String ret = null;
        try {
            String currentDate = CoreDateUtils.yyyymmdd(new Date());

            String seq = StringUtils.leftPad(
                String.valueOf(paycoreDefaultSequenceFactory.getNextValue(PAYCORE_PAY_INFO_ID)), 8,
                '0');// 16位流水,左补0
            ret = currentDate + SystemCode.PAYCORE.code() + seq;
            return ret;
        } catch (Exception e) {
            throw new RuntimeException("支付流水号生成异常", e);
        }

    }

    /**
     * 支付子流水号,规则：日期14位+3位系统编码+8位流水号
     * 
     * @return
     */
    public String genPaySubInfoId() {
        Assert.notNull(paycoreDefaultSequenceFactory);

        String ret = null;
        try {
            String currentDate = CoreDateUtils.yyyymmdd(new Date());
            String seq = StringUtils
                .leftPad(String.valueOf(paycoreDefaultSequenceFactory
                    .getNextValue(PAYCORE_PAY_SUB_INFO_ID)), 8, '0');// 16位流水,左补0
            ret = currentDate + SystemCode.PAYCORE.code() + seq;
            return ret;
        } catch (Exception e) {
            throw new RuntimeException("支付子流水号生成异常", e);
        }

    }

    /**
     * 交换子流水号,规则：日期14位+3位系统编码+8位流水号
     * 
     * @return
     */
    public String genFluxInfoId() {
        Assert.notNull(paycoreDefaultSequenceFactory);

        String ret = null;
        try {
            String currentDate = CoreDateUtils.yyyymmdd(new Date());
            String seq = StringUtils.leftPad(
                String.valueOf(paycoreDefaultSequenceFactory.getNextValue(FLUX_INFO_ID)), 8, '0');// 16位流水,左补0
            ret = currentDate + SystemCode.PAYCORE.code() + seq;
            return ret;
        } catch (Exception e) {
            throw new RuntimeException("交换流水号生成异常", e);
        }

    }

    /**
     * 机构流水号,规则：日期14位+3位系统编码+8位流水号
     * 
     * @return
     */
    public String genInstSerialNo(String channelId) {
        Assert.notNull(paycoreDefaultSequenceFactory);

        String ret = null;
        try {
            String currentDate = CoreDateUtils.yyyymmdd(new Date());
            String seq = StringUtils.leftPad(
                String.valueOf(paycoreDefaultSequenceFactory.getNextValue(FLUX_INFO_ID)), 8, '0');// 16位流水,左补0
            ret = currentDate + SystemCode.PAYCORE.code() + seq;
            return ret;
        } catch (Exception e) {
            throw new RuntimeException("交换流水号生成异常", e);
        }

    }

    /**
     * 生成批次号
     * 
     * @return
     */
    public String genBatchNo() {
        Assert.notNull(paycoreDefaultSequenceFactory);

        try {
            String seq = StringUtils.leftPad(
                String.valueOf(paycoreDefaultSequenceFactory.getNextValue(BATCH_NO)), 8, '0');// 16位流水,左补0
            return seq;
        } catch (Exception e) {
            throw new RuntimeException("批次号生成异常", e);
        }

    }

}
